package com.cherrish.demo;

import java.lang.ref.SoftReference;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author cherrish
 * @time 2019-04-17 17:03
 * @name TestLocalDateTime
 * @desc:
 */
public class TestLocalDateTime {
    public static void main(String[] args) {
        System.out.println(formatTime(convertDateToLDT(new Date()), "yyyy-MM-dd HH:mm:ss"));
    }

    public static String formatTime(LocalDateTime time, String pattern) {
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    //Date转换为LocalDateTime
    public static LocalDateTime convertDateToLDT(Date date) {
        return LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
    }

    //LocalDateTime转换为Date
    public static Date convertLDTToDate(LocalDateTime time) {
        return Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
    }
}
